<?php

namespace common\models\task;

use common\helpers\Html;
use common\models\bonus\Bonus;
use common\models\forms\CreditsLogForm;
use common\models\member\Member;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\NotFoundHttpException;

/**
 * This is the model class for table "{{%task_list}}".
 *
 * @property int $id
 * @property int $member_id
 * @property int $task_id
 * @property int $status
 * @property string $bonus
 * @property int $created_at
 * @property int $updated_at
 * @property int $today_time
 */
class TaskList extends \yii\db\ActiveRecord
{
    const ENABLED = 0;
    const WAIT = 1;
    const DONE = 2;

    public $_level;
    /**
     * @return array
     */
    public static function getMap(): array
    {
        return [
            self::ENABLED => '待提交',
            self::WAIT => '审核中',
            self::DONE => '已完成',
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%task_list}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['member_id', 'task_id'], 'required'],
            [['member_id', 'task_id', 'status', 'created_at', 'updated_at', 'today_time'], 'integer'],
            [['bonus'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'member_id' => Yii::t('app', 'Member ID'),
            'task_id' => Yii::t('app', 'Task ID'),
            'username' => Yii::t('app', '会员编号'),
            'status' => Yii::t('app', '状态'),
            'bonus' => Yii::t('app', '奖金'),
            'created_at' => Yii::t('app', '创建时间'),
            'updated_at' => Yii::t('app', '完成时间'),
            'today_time' => Yii::t('app', 'Today Time'),
        ];
    }

    public static function StatusValue($status = 1, $options = [])
    {
       

        $listBut = [
            self::ENABLED => Html::tag('span', self::getMap()[$status], array_merge(
                [
                    'class' => "label label-default label-sm",
                ],
                $options
            )),
            self::WAIT => Html::tag('span', self::getMap()[$status], array_merge(
                [
                    'class' => "label label-info label-sm",
                ],
                $options
            )),
            self::DONE => Html::tag('span', self::getMap()[$status], array_merge(
                [
                    'class' => "label label-success label-sm",
                ],
                $options
            )),
        ];

        return $listBut[$status] ?? '';
    }

    public function getTaskProList()
    {
        return $this->hasOne(TaskProList::class,['id'=>'task_id']);
    }
   
    public function add()
    {   
        $user = Member::findIdentity($this->member_id);
        if(!$user){
            return $this->addError('username',Yii::t('app', '会员有误')); 
        }
        $this->_level = $user->memberLevel;
        $this->username = $user->username;
        $this->bonus = $this->_level->pre_order;
        $today_time = strtotime(date('Y-m-d'));
        $this->today_time = $today_time;
        $count = self::find()->where(['member_id' => $this->member_id, 'today_time' => $today_time])->count();
        if ($count >= $this->_level->daily_tasks) {
            return $this->addError('task_id', Yii::t('app', '没有更多任务了'));
        }
        return $this->save();
    }
    //完成订单
    public function orderDone()
    {
        $member = Member::findOne($this->member_id);
        if(TaskList::updateAll(['status' => 2], ['id' => $this->id, 'status' => 1])){
            $transaction = Yii::$app->db->beginTransaction();
            try {
                Bonus::taskBonus($member,$this->bonus);
                $transaction->commit();
                return true;
            } catch (NotFoundHttpException $e) {
                $transaction->rollBack();
                echo $e->getMessage();
                return false;
            }
        }
    }
}
